package com.defysoft.waukids.album;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity_UpPhoto;
import com.defysoft.waukids.Custom_Data_Type.AlbumDetailLoad;
import com.defysoft.waukids.Custom_Data_Type.AlbumDetailReply;
import com.defysoft.waukids.Custom_Data_Type.Album_Main_Photo;
import com.defysoft.waukids.Data_Sharedpreferences;
import com.defysoft.waukids.JsonParser;
import com.defysoft.waukids.PublicConstant;
import com.defysoft.waukids.PublicSharedpreferences;
import com.defysoft.waukids.R;
import com.defysoft.waukids.Sqlite_Connect;
import com.defysoft.waukids.notice.NoticeDetailActivity;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class AlbumDetailActivity extends BaseActivity_UpPhoto implements
		OnClickListener {
	String albumCode;
	int photo_count = 0;
	AlbumDetailLoad albumDetailLoad;
	ArrayList<Album_Main_Photo> photoList;
	ArrayList<AlbumDetailReply> replyList;
	ImageView imageView[] = new ImageView[10];
	LinearLayout linearLayout[] = new LinearLayout[10];
	TextView textView[] = new TextView[4];
	Sqlite_Connect sc;
	Intent intent;
	Toolbar toolbar;
	// 스피너
	Spinner spinner;
	String[] celebrities = { "수정", "삭제" };
	public Sqlite_Connect sqlite_Connect;
	SharedPreferences.Editor editor;
	PublicSharedpreferences psf;
	Data_Sharedpreferences.SF_Member member;
	Data_Sharedpreferences.SF_Student student;
	// 이미지업로드
	DisplayImageOptions options;
	ImageLoader imageLoader;

	// 1분전,1일전,
	String now_date;
	long now = System.currentTimeMillis();
	Date date = new Date(now);
	SimpleDateFormat CurYearFormat ;
	SimpleDateFormat CurMonthFormat;
	SimpleDateFormat CurDayFormat ;
	SimpleDateFormat CurHourFormat ;
	SimpleDateFormat CurMinuteFormat;
	SimpleDateFormat CurSecondFormat;
	// 하단
	int stamp_a_count, stamp_b_count, stamp_c_count = 0;
	Button button[] = new Button[1];
	// 미확인 부모 리스트
	Handler mHandler;
	// 덧글용 추가.
	EditText editText;
	LinearLayout replyLinearLayout;
	ListView replyListView;
	AlbumDetailReplyAdapter replyAdapter;
	ScrollView scrollView;
	InputMethodManager ipm;
	Button replyButton;

	private final int FIRST_THREAD = 0;
	private final int STAMP_A_THREAD = 1;
	private final int STAMP_B_THREAD = 2;
	private final int STAMP_C_THREAD = 3;
	private final int DELETE_THREAD = 4;
	private final int SUCCESS_DELETE = 5;
	private final int FAIR_DELETE = 6;
	private final int SUCCESS_STAMP = 7;
	private final int FAIR_STAMP = 8;
	private final int SUCCESS_FIRST = 9;
	private final int FAIR_FIRST = 10;
	private final int NOREAD_THREAD = 11;
	private final int SUCCESS_NOREAD = 12;
	private final int FAIR_NOREAD = 13;
	private final int REPLY_THREAD = 14;
	private final int SUCCESS_REPLY = 15;
	private final int FAIR_REPLY = 16;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case START_ALBUM_DETAIL: // 9210
			if (resultCode == REFRESH_CODE) { // 5555.
				setResult(REFRESH_CODE);
				setStartData();
				startNetwork(FIRST_THREAD);
			}
			break;

		default:
			break;
		}
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		if (getIntent().getStringExtra("PK_Alb_Code") == null
				|| getIntent().getStringExtra("PK_Alb_Code") == "") {
			albumCode = "2";
		} else {
			albumCode = getIntent().getStringExtra("PK_Alb_Code");
		}
		setContentView(R.layout.albumdetail_activity);
		setStartData();
		setImgSetting();
		startNetwork(FIRST_THREAD);
		setStartToolbar();
		registerReceiver(mToastMessageReceiver, new IntentFilter(
				"com.defysoft.waukids.TOAST_MESSAGE"));
	}

	@Override
	public void setStartData() {
		super.setStartData();
		sqlite_Connect = new Sqlite_Connect(this);
		CurYearFormat = new SimpleDateFormat("yyyy"); 
		CurMonthFormat = new SimpleDateFormat("MM");  
		CurDayFormat = new SimpleDateFormat("dd");    
		CurHourFormat = new SimpleDateFormat("HH");   
		CurMinuteFormat = new SimpleDateFormat("mm"); 
		CurSecondFormat = new SimpleDateFormat("ss"); 
		photoList = new ArrayList<Album_Main_Photo>();
		psf = new PublicSharedpreferences(this);
		psf.setMembersp();
		member = psf.getMembersp();

			psf.setStudentsp();
			student = psf.getStudentsp();

		sc = new Sqlite_Connect(this);
		ipm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
		// 미확인 부모 리스트
		mHandler = new Handler();
		/** 앨범 제목 */
		textView[0] = (TextView) findViewById(R.id.ALBUM_DETAIL_NAME);
		/** 앨범 내용 */
		textView[1] = (TextView) findViewById(R.id.ALBUM_DETAIL_CONTENTS);
		/** 앨범 지난 시간 */
		textView[2] = (TextView) findViewById(R.id.ALBUM_DETAIL_ELAPSED_TIME);
		/** 앨범 제목 */
		textView[3] =(TextView) findViewById(R.id.ALBUM_DETAIL_TITLE);
		
		/** 앨범 사진 */
		imageView[0] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_1);
		imageView[1] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_2);
		imageView[2] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_3);
		imageView[3] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_4);
		imageView[4] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_5);
		imageView[5] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_6);
		imageView[6] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_7);
		imageView[7] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_8);
		imageView[8] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_9);
		imageView[9] = (ImageView) findViewById(R.id.ALBUM_DETAIL_IMG_10);
		// 사진 리니어레이아웃.
		linearLayout[0] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_1_LL);
		linearLayout[1] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_2_LL);
		linearLayout[2] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_3_LL);
		linearLayout[3] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_4_LL);
		linearLayout[4] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_5_LL);
		linearLayout[5] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_6_LL);
		linearLayout[6] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_7_LL);
		linearLayout[7] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_8_LL);
		linearLayout[8] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_9_LL);
		linearLayout[9] = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_IMG_10_LL);
		// 하단 스탬프.
		/*button[0] = (Button) findViewById(R.id.ALBUM_DETAIL_BUTTON_UNIDENTIFIED_PARENTS);*/
		button[0] = (Button) findViewById(R.id.ALBUM_DETAIL_BUTTON_STAMP_A);
		/*button[2] = (Button) findViewById(R.id.ALBUM_DETAIL_BUTTON_STAMP_B);
		button[3] = (Button) findViewById(R.id.ALBUM_DETAIL_BUTTON_STAMP_C);*/


		if (member.Rank.equals("3")) {
			button[0].setOnClickListener(this);
			/*button[2].setOnClickListener(this);
			button[3].setOnClickListener(this);*/
			
			sc.delete_New_Photo(Integer.parseInt(albumCode));
		}

		// 사진 클릭시
		for (int i = 0; i < 10; i++) {
			final int ii = i;
			imageView[i].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					intent = new Intent(AlbumDetailActivity.this,
							AlbumDetailPhoto.class).putExtra(
							"photo",
							PublicConstant.HTTP_URL_ALBUMPHOTO
									+ photoList.get(ii).Alb_Pho_IMG).putExtra(
							"photoname", photoList.get(ii).Alb_Pho_IMG);
					startActivity(intent);
				}
			});
		}

		toolbar = (Toolbar) findViewById(R.id.ALBUM_DETAIL_TOOLBAR);
		spinner = (Spinner) findViewById(R.id.ALBUM_DETAIL_SPI);
		// 하단 버튼.
		replyButton = (Button) findViewById(R.id.ALBUM_DETAIL_BUTTON_REPLY);
		replyButton.setOnClickListener(this);
		editText = (EditText) findViewById(R.id.ALBUM_DETAIL_EDITTEXT_REPLY);
		// 댓글용 추가
		replyLinearLayout = (LinearLayout) findViewById(R.id.ALBUM_DETAIL_LINEARLAYOUT_REPLY);
		replyListView = (ListView) findViewById(R.id.ALBUM_DETAIL_LISTVIEW_REPLY);
		scrollView = (ScrollView) findViewById(R.id.ALBUM_DETAIL_SCROLLVIEW);
	}

	private void setStartToolbar() {
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		spinner.setAdapter(new ArrayAdapter<String>(this, 0) {
			@Override
			public int getCount() {
				return celebrities.length;
			}

			@Override
			public View getDropDownView(int position, View convertView,
					ViewGroup parent) {
				final int pos = position;
				if (convertView == null) {
					convertView = getLayoutInflater().inflate(
							R.layout.overflowitem_row, null);
				}
				((TextView) convertView.findViewById(R.id.OVERFLOWITEM_TV))
						.setText(celebrities[position]);
				convertView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						switch (pos) {
						case 0: // 수정하기
							Intent i = new Intent(AlbumDetailActivity.this,
									Album_Detail_Edit.class).putExtra(
									"PK_Alb_Code", albumDetailLoad.PK_Alb_Code);
							startActivityForResult(i, START_ALBUM_DETAIL);
							break;
						case 1: // 삭제하기
							alertDialog("삭제 하시겠습니까?", DELETE_THREAD);
							break;
						/*case 2:	//미확인부모리스트
							startNetwork(NOREAD_THREAD);
							break;*/

						}
					}
				});
				return convertView;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				final int pos = position;
				if (convertView == null) {
					convertView = getLayoutInflater().inflate(
							R.layout.overflowitem_row, null);
				}
				((TextView) convertView.findViewById(R.id.OVERFLOWITEM_TV))
						.setBackgroundColor(0x00ffffff);
				return convertView;
			}
		});
	}

	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho = choice;
		firstThread = new Thread(new Runnable() {
			@Override
			public void run() {
				switch (cho) {
				case FIRST_THREAD:
					albumDetailThread();
					break;
				case STAMP_A_THREAD:
					stampThread("Alb_Stamp_A", stamp_a_count + 1);
					break;
/*				case STAMP_B_THREAD:
					stampThread("Alb_Stamp_B", stamp_b_count + 1);
					break;
				case STAMP_C_THREAD:
					stampThread("Alb_Stamp_C", stamp_c_count + 1);
					break;*/
				case DELETE_THREAD:
					deleteThread();
					break;
				case NOREAD_THREAD:
					noReadThread();
					break;
				case REPLY_THREAD:
					replyThread();
					break;
				}
			}
		});
		firstThread.start();
	}

	protected void replyThread() {
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_ALBUM_DETAIL_REPLY + "&FK_Sch_Alb_Rep_Code="
							+ albumDetailLoad.FK_Sch_Alb_Code
							+ "&FK_Cla_Alb_Rep_Code="
							+ albumDetailLoad.FK_Cla_Alb_Code
							+ "&FK_Alb_Alb_Rep_Code="
							+ albumDetailLoad.PK_Alb_Code + "&FK_Mem_Alb_Rep_Email="
							+ member.Email + "&FK_Stu_Alb_Rep_Code="
							+ student.Code + "&Alb_Rep_Sub="
							+ editText.getText().toString()));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_REPLY, "");
			} else {
				onNetworkCallback(FAIR_REPLY, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString()+"where replyThread()");
		
		}
	}

	protected void deleteThread() {
		String msg = "";
		
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_ALBUM_DETAIL_DELETE + "&PK_Alb_Code="
							+ albumCode));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_DELETE,
						jsonObject.getString("payload"));// 다시 받아오지 말고, 눌리면 바로
															// 숫자1올라가고,
			} else {
				onNetworkCallback(FAIR_DELETE, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString()+"where deleteThread()");
		}
	}

	protected void stampThread(String string, int count) {
		final String stamp = string;
		final int stampCount = count;
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_ALBUM_DETAIL_STAMP + "&PK_Alb_Code="
							+ albumCode + "&Alb_Stamp_=" + stamp
							+ "&stampCount=" + stampCount));
			if (jsonObject.getBoolean("success")) {
				albumDetailLoad = JsonParser.albumDetailLoad(jsonObject
						.getString("payload"));
				onNetworkCallback(SUCCESS_STAMP,
						jsonObject.getString("payload"));

			} else {
				onNetworkCallback(FAIR_STAMP, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString()+"where stampThread()");
		}
	}

	protected void albumDetailThread() {
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_ALBUM_DETAIL_LOAD + "&PK_Alb_Code="
							+ albumCode));
			if (jsonObject.getBoolean("success")) {
				albumDetailLoad = JsonParser.albumDetailLoad(jsonObject
						.getString("payload"));
				this.photoList = JsonParser.Album_Main_Photo_Parser(jsonObject
						.getString("photo"));
				this.photo_count = jsonObject.getInt("count");
				this.now_date = jsonObject.getString("nowdate"); // 현재시간
				this.replyList = JsonParser.albumReplyList(jsonObject
						.getString("reply"));

				onNetworkCallback(SUCCESS_FIRST, "");
			} else {
				onNetworkCallback(FAIR_FIRST, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString()+"where albumDetailThread()");
		}
	}

	public void noReadThread() {
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_ALBUM_DETAIL_NOREAD + "&pk=" + albumCode));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_NOREAD,
						jsonObject.getString("payload"));
			} else {
				onNetworkCallback(FAIR_NOREAD, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString()+" where noReadThread()");
		}
	}

	@Override
	protected void handlingMessage(Message _message) {
		super.handlingMessage(_message);
		switch (_message.what) {
		case SUCCESS_FIRST:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			//선생님글인지 원장님 글인지.
			textView[0].setText(albumDetailLoad.Alb_Writer);
			
			/*if (albumDetailLoad.Cla_Name.equals("") ||albumDetailLoad.Cla_Name.equals("null")) {
				textView[0].setText(albumDetailLoad.Sch_Name+" 원장님");
			} else {
				textView[0].setText(albumDetailLoad.Cla_Name+" 선생님");		
			}*/
			
			textView[1].setText(albumDetailLoad.Alb_Sub);
			textView[3].setText(albumDetailLoad.Alb_Name);
			stamp_a_count = Integer.parseInt(albumDetailLoad.Alb_Stamp_A);
/*			stamp_b_count = Integer.parseInt(albumDetailLoad.Alb_Stamp_B);
			stamp_c_count = Integer.parseInt(albumDetailLoad.Alb_Stamp_C);*/
				button[0].setText(Integer.toString(stamp_a_count));
/*			button[2].setText(Integer.toString(stamp_b_count));
			button[3].setText(Integer.toString(stamp_c_count));*/
			// 작성자만 스피너 보이게.
			if (member.Email.equals(albumDetailLoad.FK_Mem_Alb_Email)) {
				spinner.setVisibility(View.VISIBLE); // 보이게.
			}
			timeCalculation(); // 경과시간계산기.
			if (photo_count > 10) {
				photo_count = 10;
			}
			if (photo_count >= 0) {
				for (int i = 0; i < photo_count; i++) {
					/* imageView[i].setVisibility(View.VISIBLE); */
					linearLayout[i].setVisibility(View.VISIBLE);
					imageLoader.displayImage(PublicConstant.HTTP_URL_ALBUMPHOTO
							+ photoList.get(i).Alb_Pho_IMG, imageView[i],
							options, null);
				}
			}
			// 덧글용
			replyAdapter = new AlbumDetailReplyAdapter();
			replyListView.setAdapter(replyAdapter);
			// 리스트길이보이게
			setListViewHeightBasedOnChildren(replyListView);
			// 강제로 터치스크롤 세팅.1.
			/*
			 * replyListView.setOnTouchListener(new OnTouchListener() {
			 * 
			 * @Override public boolean onTouch(View v, MotionEvent event) {
			 * scrollView.requestDisallowInterceptTouchEvent(true); return
			 * false; } });
			 */
			/*
			 * LayoutInflater layoutInflater = getLayoutInflater(); LinearLayout
			 * layout =
			 * (LinearLayout)layoutInflater.inflate(R.layout.diarydetail_activity
			 * , null); replyListView.addFooterView(layout);
			 */
			break;
		case FAIR_FIRST:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_LONG)
					.show();
			setResult(REFRESH_CODE);
			finish();
			break;

		case SUCCESS_STAMP:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			stamp_a_count = Integer.parseInt(albumDetailLoad.Alb_Stamp_A);
/*			stamp_b_count = Integer.parseInt(albumDetailLoad.Alb_Stamp_B);
			stamp_c_count = Integer.parseInt(albumDetailLoad.Alb_Stamp_C);*/
			button[0].setText(Integer.toString(stamp_a_count));
/*			button[2].setText(Integer.toString(stamp_b_count));
			button[3].setText(Integer.toString(stamp_c_count));*/
			sc.insert_PhotoStamp(Integer.parseInt(albumCode),
					Integer.parseInt(student.Code));
			break;

		case FAIR_STAMP:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			break;
		case SUCCESS_DELETE:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			setResult(REFRESH_CODE);
			finish();
			break;

		case FAIR_DELETE:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			break;

		case SUCCESS_NOREAD: // 미확인부모리스트
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			String testmsg = (String) _message.obj;
			String[] testlist = testmsg.split(",");
			testmsg = "";
			for (int i = 0; i < testlist.length; i++) {
				testmsg += testlist[i] + "\n";
			}
			showPopup("미확인 부모님", testmsg, 0);
			break;

		case FAIR_NOREAD: // 미확인부모리스트
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			break;
		case SUCCESS_REPLY:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			setResult(REFRESH_CODE);
			startNetwork(FIRST_THREAD);
			editText.setText("");
			// 키보드 내리기.
			ipm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

			break;
		case FAIR_REPLY:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(this, "FAIR_REPLY=" + (String) _message.obj,
					Toast.LENGTH_SHORT).show();
			break;
		case CATCH_EXCEPTION:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			sqlite_Connect.insert_Err_Log((String)_message.obj,android.os.Build.VERSION.SDK_INT,"AlbumDetalActivity", member.Email);
			break;

		}
	}

	public void setImgSetting() {
		// 이미지 세팅.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				this).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder().cacheInMemory(true)
		/* .imageScaleType(ImageScaleType.EXACTLY) */
		.cacheOnDisc(true).considerExifParams(true).build();
	}

	/** 경과일자시간계산기 */
	protected void timeCalculation() {
		// 시간 계산기. split
		
		String album_date = albumDetailLoad.Alb_Date;
		// 1.앨범사항 시간.
		String alb_date = album_date.split(" ")[0];
		String alb_time = album_date.split(" ")[1];
		// 일월년 분할.
		int alb_year = Integer.parseInt(alb_date.split("-")[0]);
		int alb_month = Integer.parseInt(alb_date.split("-")[1]);
		int alb_day = Integer.parseInt(alb_date.split("-")[2]);
		// 시분초 분할.
		int alb_hour = Integer.parseInt(alb_time.split(":")[0]);
		int alb_minute = Integer.parseInt(alb_time.split(":")[1]);
		int alb_second = Integer.parseInt(alb_time.split(":")[2]);

		// 2. 현재날짜시간
		int now_year = Integer.parseInt(CurYearFormat.format(date));
		int now_month = Integer.parseInt(CurMonthFormat.format(date));
		int now_day = Integer.parseInt(CurDayFormat.format(date));
		int now_hour = Integer.parseInt(CurHourFormat.format(date));
		int now_minute = Integer.parseInt(CurMinuteFormat.format(date));
		int now_second = Integer.parseInt(CurSecondFormat.format(date));

		// 3. 계산기 시작.
		if (alb_year < now_year) {
			textView[2].setText(alb_date);
		} else if (alb_month < now_month) {
			int a = (now_month - alb_month);
			textView[2].setText(a + " 개월 전");
		} else if (alb_day < now_day) {
			int aa = (now_day - alb_day);
			textView[2].setText(aa + " 일 전");
		} else if (alb_hour < now_hour) {
			int aaa = (now_hour - alb_hour);
			textView[2].setText(aaa + " 시간 전");
		} else if (alb_minute < now_minute) {
			int aaaa = (now_minute - alb_minute);
			textView[2].setText(aaaa + " 분 전");
		} else if (alb_minute == now_minute) {
			textView[2].setText("방금 전");

		}
		// 계산기 끝.
		/** 하단 미확인부모, 스탬프3개 */

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		/*case R.id.ALBUM_DETAIL_BUTTON_UNIDENTIFIED_PARENTS: // 미확인부모리스트
			startNetwork(NOREAD_THREAD);
			break;*/
		case R.id.ALBUM_DETAIL_BUTTON_STAMP_A: // 스탬프A
			if (!sc.stamp_Photo(Integer.parseInt(albumCode),
					Integer.parseInt(student.Code))) {
				alertDialog("좋아요를 등록하겠습니까?", STAMP_A_THREAD);
			}else {
				Toast.makeText(this, "이미 선택한 글입니다.", Toast.LENGTH_SHORT).show();
			}
			break;
		/*case R.id.ALBUM_DETAIL_BUTTON_STAMP_B: // 스탬프B
			if (!sc.stamp_Photo(Integer.parseInt(albumCode),
					Integer.parseInt(student.Code))) {
				alertDialog("스탬프B?", STAMP_B_THREAD);
			}else {
				Toast.makeText(this, "이미 선택한 글입니다.", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.ALBUM_DETAIL_BUTTON_STAMP_C: // 스탬프C
			if (!sc.stamp_Photo(Integer.parseInt(albumCode),
					Integer.parseInt(student.Code))) {
				alertDialog("스탬프C?", STAMP_C_THREAD);
			}else {
				Toast.makeText(this, "이미 선택한 글입니다.", Toast.LENGTH_SHORT).show();
			}
			break;*/
		case R.id.ALBUM_DETAIL_BUTTON_REPLY: // 덧글쓰기.
			if (editText.getText().length() <= 0) {
				Toast.makeText(this, "댓글 내용을 작성해 주세요.",Toast.LENGTH_SHORT).show();
			} else {
				alertDialog(editText.getText().toString(), REPLY_THREAD);
			}
			break;
		}
	}

	// 네.아니오.선택다이얼로그
	void alertDialog(String string_title, int int_code) {
		final String title = string_title;
		final int code = int_code;
		AlertDialog.Builder builder = new AlertDialog.Builder(
				AlbumDetailActivity.this);
		AlertDialog alert;
		builder.setMessage(title)
				.setCancelable(false)
				.setPositiveButton("네", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						startNetwork(code);
					}
				})
				.setNegativeButton("아니오",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});
		alert = builder.create();
		alert.show();
	}

	// 댓글용 어댑터
	class AlbumDetailReplyAdapter extends ArrayAdapter<String> {

		public AlbumDetailReplyAdapter() {
			super(AlbumDetailActivity.this, 0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return replyList.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AlbumDetailReplyHolder holder;
			final int pos = position;
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.album_detail_reply_left_item, null);
				holder = new AlbumDetailReplyHolder();
				holder.left_ly = (LinearLayout)convertView.findViewById(R.id.ALBUM_DETAIL_REPLY_LEFT_CONTAINER);
				holder.right_ly = (LinearLayout)convertView.findViewById(R.id.ALBUM_DETAIL_REPLY_RIGHT_CONTAINER);
				
				holder.left_replySub = (TextView) convertView
						.findViewById(R.id.ALBUM_DETAIL_REPLY_LEFT_SUB);
				holder.left_replyDate = (TextView) convertView
						.findViewById(R.id.ALBUM_DETAIL_REPLY_LEFT_DATE);
				holder.left_replyName = (TextView) convertView
						.findViewById(R.id.ALBUM_DETAIL_REPLY_LEFT_NAME);
				
				holder.right_replySub = (TextView) convertView
						.findViewById(R.id.ALBUM_DETAIL_REPLY_RIGHT_SUB);
				holder.right_replyDate = (TextView) convertView
						.findViewById(R.id.ALBUM_DETAIL_REPLY_RIGHT_DATE);
				holder.right_replyName = (TextView) convertView
						.findViewById(R.id.ALBUM_DETAIL_REPLY_RIGHT_NAME);
				
				convertView.setTag(holder);
			} else {
				holder = (AlbumDetailReplyHolder) convertView.getTag();
			}
			// 내용넣기.
			if(!replyList.get(position).FK_Mem_Alb_Rep_Email.equals(member.Email)){
				holder.right_ly.setVisibility(View.VISIBLE);
				holder.right_replySub.setText(replyList.get(position).Alb_Rep_Sub);
				holder.right_replyDate.setText(replyList.get(position).Alb_Rep_Date);
				holder.right_replyName.setText(replyList.get(position).Alb_Writer);
			
					}else{
			
				holder.left_ly.setVisibility(View.VISIBLE);
				holder.left_replySub.setText(replyList.get(position).Alb_Rep_Sub);
				holder.left_replyDate.setText(replyList.get(position).Alb_Rep_Date);
				holder.left_replyName.setText(replyList.get(position).Alb_Writer);
			}
			
			
			return convertView;
		}

	}// ADAPTER_END

	class AlbumDetailReplyHolder {
		TextView left_replySub, left_replyDate, left_replyName;
		TextView right_replySub, right_replyDate, right_replyName;
		LinearLayout left_ly,right_ly;
	}

	private void setListViewHeightBasedOnChildren(ListView replyListView2) {
		ListAdapter listAdapter = replyListView2.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listitem = listAdapter.getView(i, null, replyListView2);
			listitem.measure(0, 0);
			totalHeight += listitem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = replyListView2.getLayoutParams();
		params.height = totalHeight
				+ (replyListView2.getDividerHeight() * (listAdapter.getCount() - 1));
		replyListView2.setLayoutParams(params);
		replyListView2.requestLayout();
	}

	private final BroadcastReceiver mToastMessageReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.toast_layout, null);
			TextView toast_tv = (TextView)layout.findViewById(R.id.TOAST_LAYOUT_TV);
			Toast toast = new Toast(AlbumDetailActivity.this);
			toast.setView(layout);
			toast_tv.setText(intent.getStringExtra("PUSH_NAME")+" : "+intent.getStringExtra("PUSH_SUB"));
			toast.setDuration(Toast.LENGTH_LONG);
			toast.show();
		}
	};

}// CLASS_END
